<template>
  <!-- 记录设备归返管理 -->
  <base-breadcrumb :title="$route.meta.title">
    <searchData :searchData="searchArr" @search="search"></searchData>
    <a-card :bordered="false">
      <list-table ref="table" :loading="loading" tableLayout="fixed" size="default"
                  :columns="columns" :request="loadData"
                  :statistics-config="{ use: true, statistics, temp }"
                  :selectConfig="{ use: true, type: 'checkbox', rowKey: 'id' }" :export-config="{
                            use: true,
                            all: exportConfig.all,
                            select: exportConfig.select,
                        }" :export-config-text="{ select: '导出选中', all: '导出全部' }"
                  :exportPermission="{select: 'qm_066_aexp_04',all: 'qm_066_sexp_03'}"
                  @exportSelect="exportSelect" @exportAll="exportAll">
        <template slot="prefix">
          <a-button icon="plus" type="primary" v-if="hideBuquBtn()" @click="handleManage(1)"
                    v-btnPermission="'qm_066_add_01'">新增归还</a-button>
        </template>
        <template slot="actions" slot-scope="text, record">
          <MoreButton>
            <span v-btnPermission="'qm_066_loo_02'">
              <a @click="handleManage(0, record)">查看</a>
            </span>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import api from '@/api/quality/recordDeviceReturnManagement' // api
import OrgTreeSelect from '@/components/OrgTreeSelect'
import handleDownload from '@/views/quality/equipmentManagement/constant/downloadFile.js'
export default {
  name: "recordDeviceReturnManagementList",
  data() {
    return {
      loading: false,
      queryParam: {},
      editModel: 1,
      exportConfig: {
        all: '记录设备归返管理-导出全部',
        select: '记录设备归返管理-导出选中',
      },
      api,//api
      // 搜索框
      searchArr: [
        {
          name: '创建人',
          type: 'input',
          value: 'creator',
        },
        {
          name: '创建时间',
          type: 'dateRange',
          value: 'createdTime',
        },
      ],
      columns: [
        {
          title: '序号',
          width: 80,
          dataIndex: 'index',
          customRender() {
            return arguments[2] + 1
          }
        },
        {
          title: '分公司',
          ellipsis: true,
          dataIndex: 'branchCompanyName'
        },
        {
          title: '项目名称',
          ellipsis: true,
          dataIndex: 'projectName'
        },
        {
          title: '归还设备数量',
          ellipsis: true,
          dataIndex: 'returnNum'
        },
        {
          title: '最终归还设备数量',
          ellipsis: true,
          dataIndex: 'confirmReturnNum'
        },
        {
          title: '创建人',
          ellipsis: true,
          dataIndex: 'creator'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          dataIndex: 'actions',
          width: '230px',
          fixed: 'right',
          scopedSlots: { customRender: 'actions' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        let { pageNo, pageSize } = parameter;
        let params = {
          pageNum: pageNo,
          pageSize
        };
        const requestParameters = Object.assign({}, params, { param: this.setQueryParam() })
        this.loading = true;
        return this.api.list(requestParameters).then((res) => {
          this.loading = false;
          if (res.code === '0') {
            return { code: 200, data: res.data?.list, totalSize: res.data?.total }
          }
        }).catch((err) => {
          this.loading = false;
        })
      },
      totalSize: 0,
      statistics: {
        returnNum: 0,
        confirmReturnNum: 0,
      },
      temp: () => {
        return (
          <span style="margin-right: 12px">
            归还设备数量：<span class="totalNum">{this.statistics.returnNum || 0} 个</span> |
            最终归还设备数量 ：<span class="totalNum">{this.statistics.confirmReturnNum || 0} 个</span>
          </span>
        )
      },
    }
  },
  created() {
    //不是业务查询模块才展示
    if (this.hideBuquBtn()) {
      this.searchArr.unshift({
        name: '所属单位',
        valueKey: 'searchQuery',
        type: OrgTreeSelect,
      })
    }
    this.getCountByMdStatus();
  },
  methods: {
    /**
     * @description: 设置查询参数
     */
    setQueryParam() {
      if (this.queryParam.createdTime && this.queryParam.createdTime.length > 0) {
        this.queryParam.createStartTime = this.queryParam.createdTime[0].format('YYYY-MM-DD') + ' 00:00:00'
        this.queryParam.createEndTime = this.queryParam.createdTime[1].format('YYYY-MM-DD') + ' 23:59:59'
        delete this.queryParam.createdTime
      }
      return this.queryParam
    },
    /**
     * @description: 获取统计数量
     */
    async getCountByMdStatus() {
      let res = await this.api.getCountByMdStatus(this.setQueryParam());
      if (res.code === '0') {
        this.statistics = res.data;
      }
    },
    /**
      * @description: 导出选中
      * @param {Arrary} data 选中的数据
      */
    exportSelect(data) {
      const idList = data.map(item => item.id)
      const requestParameters = Object.assign({}, this.setQueryParam())
      requestParameters.ids = idList;
      handleDownload(this.api.exportData, requestParameters, this.exportConfig.select)
    },
    /**
    * @description: 导出全部
    */
    exportAll() {
      handleDownload(this.api.exportData, this.setQueryParam(), this.exportConfig.all)
    },
    /**
    * @description: 搜索
    * @param {Object} val 筛选项
    */
    search(val) {
      this.queryParam = { ...val }
      this.getCountByMdStatus();
      this.$refs.table.refresh()
    },
    /**
    * @description: 新增归还/查看
    * @param {Number} type 1:新增归还 0:查看
    * @param {Object} record 当前行数据
    */
    handleManage(type, record) {
      this.editModel = type
      let query = {
        editModel: type
      }
      if (record && record.id) {
        query.id = record.id;
      }
      this.$router.push({
        name: 'recordDeviceReturnManagementInfo',
        query
      })
    }
  }
}
</script>

<style>
</style>